package com.example.jh352160.surfingtv.mypage.childPage;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.mypage.customView.MyPageSettingItem;
import com.example.jh352160.surfingtv.network.bean.BindTelephoneBean;
import com.example.jh352160.surfingtv.network.item.RequestItem;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.MyDataService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by jh352160 on 2016/7/27
 */

public class BindTelephone extends AppCompatActivity {

    MyPageSettingItem telephone;
    MyPageSettingItem identify;
    MyPageSettingItem password;
    Button submit, sendSMS;
    Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mypage_setting_bindphone);
        telephone = (MyPageSettingItem) findViewById(R.id.phoneNumber);
        identify = (MyPageSettingItem) findViewById(R.id.identify);
        password = (MyPageSettingItem) findViewById(R.id.password);
        submit = (Button) findViewById(R.id.submit);
        sendSMS = (Button) telephone.findViewById(R.id.button);
        telephone.setView("手机号码", "请输入手机号", InputType.TYPE_CLASS_NUMBER, true);
        identify.setView("验证码", "请输入验证码", InputType.TYPE_CLASS_NUMBER);
        password.setView("登录密码", "请输入密码", InputType.TYPE_TEXT_VARIATION_PASSWORD);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("");

        sendSMS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText = (EditText) telephone.findViewById(R.id.edittext);
                Retrofit retrofit = CommonRetrofit.getRetrofit();
                MyDataService myDataService = retrofit.create(MyDataService.class);
                String[] strings = new String[]{editText.getText().toString()};
                Call<RequestItem> call = myDataService.sendSMS(strings);
                call.enqueue(new Callback<RequestItem>() {
                    @Override
                    public void onResponse(Call<RequestItem> call, Response<RequestItem> response) {
                        if (response.body().getCode().equals("200")) {
                            Toast.makeText(BindTelephone.this, "OK", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<RequestItem> call, Throwable t) {

                    }
                });
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText telephoneEdit = (EditText) telephone.findViewById(R.id.edittext);
                EditText identifyEdit = (EditText) identify.findViewById(R.id.edittext);
                EditText passwordEdit = (EditText) password.findViewById(R.id.edittext);
                BindTelephoneBean bindTelephoneBean = new BindTelephoneBean(telephoneEdit.getText().toString(), identifyEdit.getText().toString(), passwordEdit.getText().toString());
                Retrofit retrofit = CommonRetrofit.getRetrofit();
                MyDataService myDataService = retrofit.create(MyDataService.class);
                Call<RequestItem> call = myDataService.bindTelephoneBean(bindTelephoneBean);
                call.enqueue(new Callback<RequestItem>() {
                    @Override
                    public void onResponse(Call<RequestItem> call, Response<RequestItem> response) {
                        if (response.body().getCode().equals("200")) {
                            Toast.makeText(BindTelephone.this, "绑定成功", Toast.LENGTH_LONG).show();
                            BindTelephone.this.finish();
                        }
                    }

                    @Override
                    public void onFailure(Call<RequestItem> call, Throwable t) {

                    }
                });
            }
        });
    }
}
